<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div class="tools-select">
          <el-row>
            <el-col :span="24">
              <el-form
                :inline="true"
                :model="form"
                @submit.native.prevent="handleQuery()"
              >
                <el-form-item>
                  <el-select
                    v-model="form.applyed"
                    placeholder="请选择状态"
                    clearable
                    size="mini"
                    style="width: 120px"
                    @change="handleQuery"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-input
                    v-model="form.name"
                    placeholder="购货方名称"
                    size="mini"
                    clearable
                    status-icon
                    @clear="handleQuery"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-search"
                    native-type="submit"
                    >查询
                  </el-button>
                </el-form-item>
                <el-form-item>
                  <el-button
                    v-if="$store.getters.roles.includes('SYS_NUO_INVOICE_ADD')"
                    type="info"
                    size="mini"
                    icon="el-icon-plus"
                    @click="handleAdd"
                    >新增
                  </el-button>
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="info"
                    size="mini"
                    icon="el-icon-help"
                    @click="handlerQRCode"
                    >认证
                  </el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </div>
      <!-- 表格区 -->
      <el-table size="mini" stripe :data="tableData" border>
        <el-table-column align="center" label="序号" type="index" />
        <!--  <el-table-column align="center" label="全电发票号" prop="hm" width="160px"/> -->
        <el-table-column
          align="center"
          label="购货方名称"
          prop="name"
          width="300px"
        />
        <el-table-column align="center" label="类型">
          <template v-slot="scope">
            <el-tag v-if="scope.row.pattern" size="mini">正票</el-tag>
            <el-tag v-if="!scope.row.pattern" size="mini" type="info"
              >红票</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="发票类型">
          <template v-slot="scope">
            <el-tag v-if="scope.row.type === '51'" size="mini">电子发票</el-tag>
            <el-tag v-if="scope.row.type === '61'" size="mini">全电专票</el-tag>
            <el-tag v-if="scope.row.type === '62'" type="info" size="mini"
              >全电普票</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="申请状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.applyed" size="mini">申请成功</el-tag>
            <el-tag v-if="!scope.row.applyed" type="info" size="mini"
              >未申请</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="下载状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.downloaded" size="mini">下载成功</el-tag>
            <el-tag v-if="!scope.row.downloaded" type="info" size="mini"
              >未下载</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="发送状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.sended" size="mini">发送成功</el-tag>
            <el-tag v-if="!scope.row.sended" type="info" size="mini"
              >未发送</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="金额" prop="fee" />
        <el-table-column align="center" label="开票项目" prop="item" />
        <el-table-column align="center" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="300">
          <template v-slot="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-view"
              @click="handleView(scope.row.id)"
              >查看
            </el-button>
            <el-button
              v-if="scope.row.url"
              type="text"
              size="mini"
              icon="el-icon-bottom"
              @click="handleDownload(scope.row.url)"
              >下载
            </el-button>
            <el-button
              v-if="
                $store.getters.roles.includes('SYS_NUO_INVOICE_EDIT') &&
                !scope.row.hm
              "
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.id)"
              >编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-goods"
              @click="handleRed(scope.row.id)"
              v-if="scope.row.pattern"
              >红冲
            </el-button>
            <el-button
              v-if="$store.getters.roles.includes('SYS_NUO_INVOICE_DELETE')"
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel(scope.row.id)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        :current-page="this.page.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <EditDialog ref="edit" @onCallBack="handleQuery" />
    <ViewDialog ref="view" />
    <QRCodeDialog ref="qr" />
  </div>
</template>

<script>
import { getPage, deleteById, handleRed } from "@/api/sysNuoInvoice";
import EditDialog from "./components/EditDialog";
import ViewDialog from "./components/ViewDialog";
import QRCodeDialog from "./components/QRCodeDialog";

export default {
  components: { EditDialog, ViewDialog, QRCodeDialog },
  data() {
    return {
      tableData: [],
      options: [
        {
          value: true,
          label: "已开票",
        },
        {
          value: false,
          label: "未开票",
        },
      ],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: undefined,
      },
      form: {
        name: "",
        applyed: false,
      },
    };
  },
  mounted() {
    this.handleQuery();
  },
  methods: {
    handleQuery() {
      getPage({
        pageSize: this.page.pageSize,
        pageNum: this.page.pageNum,
        name: this.form.name,
        applyed: this.form.applyed,
      }).then((res) => {
        this.tableData = res.data.records;
        this.page.total = res.data.total;
      });
    },
    handleSizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.handleQuery();
    },
    handleCurrentChange(pageNum) {
      this.page.pageNum = pageNum;
      this.handleQuery();
    },
    handleAdd() {
      this.$refs["edit"].onShow();
    },
    handleView(id) {
      this.$refs["view"].onShow(id);
    },
    handleEdit(id) {
      this.$refs["edit"].onShow(id);
    },
    handleRed(id) {
      handleRed(id).then((res) => {
        if (res.code === 20000) {
          this.handleQuery();
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    handleDel(id) {
      deleteById(id).then((res) => {
        if (res.code === 20000) {
          this.handleQuery();
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    handlerQRCode() {
      this.$refs["qr"].onShow();
    },
    handleDownload(url) {
      window.location.href = url;
    },
  },
};
</script>
<style scoped>
.el-form {
  margin-top: 20px;
}

.el-pagination {
  float: left;
  padding-top: 20px;
  padding-bottom: 20px;
}

.tools-select {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 10px;
  padding-left: 10px;
  background-color: #ffffff;
  border: 1px;
  border-radius: 5px;
}
</style>
